<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
  <title>任务列表</title>
  <div th:replace="common/link::header"></div>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
              <input type="text" name="name" placeholder="请输入名称" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">类名称</label>
            <div class="layui-input-block">
              <input type="text" name="className" placeholder="请输入类名称" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
            <button class="layui-btn layui-btn-primary" data-type="resetForm">重置</button>
            <button sec:authorize="hasAuthority('task:add')" class="layui-btn layuiadmin-btn-useradmin" data-type="addView">添加</button>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <table id="taskTable" lay-filter="taskTable"></table>
        <script type="text/html" id="enabledTpl">
          <input type="checkbox" lay-filter="enabled" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" {{d.enabled==='Y'?'checked':''}} />
        </script>
        <script type="text/html" id="tableBar">
          <a sec:authorize="hasAuthority('task:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a sec:authorize="hasAuthority('task:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
      </div>
    </div>
  </div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
  layui.config({
      base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'useradmin', 'table'], function() {
      let $ = layui.$,
              form = layui.form,
              table = layui.table;
      //监听搜索
      form.on('submit(search)', function(data){
          let field = data.field;
          //执行重载
          table.reload('taskTable', {
              where: field
          });
      });
      // 查询列表接口
      table.render({
          elem: '#taskTable'
          ,height: 'full-110'
          ,url:  ctx + '/sysTask/list'
          ,page: true //开启分页,
          ,limit: 10 // 多少条
          ,request: {
            pageName: 'pageNum', //页码的参数名称，默认：page
            limitName: 'pageSize' //每页数据量的参数名，默认：limit
          }
          ,parseData: function (result) {
              return {
                "code": result.code,
                "count": result.data.total,
                "data": result.data.records
              }
          }
          ,response: {
               statusCode: 200 //成功的状态码，默认：0
          }
          ,cols: [[ //表头
              {field: 'id', title: 'ID', sort: true, width: 80, fixed: 'left'},
              {field: 'name', title: '名称'},
              {field: 'cron', title: '表达式'},
              {field: 'className', title: '类名称'},
              {field: 'methodName', title: '方法名'},
              {field: 'enabled', align: "center", sort: true, templet: '#enabledTpl', title: '状态'},
              {toolbar: '#tableBar', title: '操作', width: 300, align:'center',fixed: 'right'}
          ]]
      });

      //监听行工具事件
      table.on('tool(taskTable)', function(obj){
          var data = obj.data //获得当前行数据
              ,layEvent = obj.event;
          if(layEvent === 'edit'){
              active.editView(data.id);
          } else if(layEvent === 'del'){
              active.del(data.id);
          }
      });

      $('.layui-btn').on('click', function(){
          let type = $(this).data('type');
          active[type] && active[type].call(this);
      });

      // 修改任务状态
      form.on('switch(enabled)', function (obj) {
        let id = obj.elem.value;
        let checked = obj.elem.checked ? 'Y' : 'N';
        active.changeTaskEnabled(id,checked);
      });

      /* 触发弹层 */
      let active = {
          /**
           * 重置
           */
          resetForm:function () {
            $(".layui-form").find("input").val("");
            form.render();
            table.reload('taskTable', {
              where: {
                name: "",
                className: ""
              }
            });
          },
          /**
           * 添加
           * */
          addView: function(){
              layer.open({
                   type: 2
                  ,shade: 0.3
                  ,title: "添加任务"
                  ,content: ctx + '/sysTask/addPage'
                  ,maxmin: true
                  ,area: ['50%', '80%']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      let submit = layero.find('iframe').contents().find('#save-submit');
                      submit.trigger('click');
                  }
              });
          },
          /**
           * 编辑
           * @param id id
           * */
          editView: function(id){
              layer.open({
                   type: 2
                  ,shade: 0.3
                  ,title: '修改任务'
                  ,content: ctx + '/sysTask/editPage/' + id
                  ,maxmin: true
                  ,area: ['50%', '80%']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      let submit = layero.find('iframe').contents().find('#save-submit');
                      submit.trigger('click');
                  }
              });
          },
          /**
           * 删除
           * @param id id
           */
          del: function(id){
              layer.confirm('真的删除行么', function(index){
                  //向服务端发送删除指令
                  $.ajax({
                      type: 'DELETE',
                      url:  ctx + '/sysTask/del/' + id,
                      contentType:'application/json;charset=UTF-8',
                      dataType: 'json',
                      success: function(result) {
                          layer.msg(result.message);
                          if (result.code === 200) {
                              layer.close(index);
                              table.reload('taskTable');
                          }
                      }
                  });
              });
          },
        /**
         * 修改任务状态
         * @param id 任务id
         * @param checked 状态
         */
        changeTaskEnabled: function (id,checked) {
          $.ajax({
            type: 'POST',
            url:  ctx + '/sysTask/changeTask',
            data: JSON.stringify({
              id: id,
              enabled: checked
            }),
            contentType:'application/json;charset=UTF-8',
            dataType: 'json',
            success: function(result) {
              layer.msg(result.message);
              if (result.code === 200) {
                table.reload('taskTable');
              }
            }
          });
        }
      }
  });
  </script>
</body>
</html>
